package com.weitu.accidentEvent.accident.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.weitu.accidentEvent.accident.mapper.AccidentReportMapper;
import com.weitu.accidentEvent.accident.mapper.CasualtiesMapper;
import com.weitu.accidentEvent.accident.mapper.HandlingSuggestionMapper;
import com.weitu.accidentEvent.accident.mapper.PrecautionMapper;
import com.weitu.accidentEvent.accident.service.AccidentReportService;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by fanyuting on 2017/5/25 0025.
 */
@Service
public class AccidentReportServiceImpl implements AccidentReportService {
    @Autowired
    private AccidentReportMapper accidentReportMapper;
    @Autowired
    private CasualtiesMapper casualtiesMapper;
    @Autowired
    private HandlingSuggestionMapper handlingSuggestionMapper;
    @Autowired
    private PrecautionMapper precautionMapper;

    @Override
    public List<Map<String, Object>> queryListByParam(Map<String, Object> map) {
        return this.accidentReportMapper.queryListByParam(map);
    }

    @Override
    public int queryListCountByParam(Map<String, Object> map) {
        return this.accidentReportMapper.queryListCountByParam(map);
    }

    @Override
    public Map<String, Object> queryInfoById(String id) {
        return this.accidentReportMapper.queryInfoById(id);
    }

    @Override
    @Transactional
    public boolean addInfo(Map<String, Object> map) {
        //保存事故报告信息
        int result = this.accidentReportMapper.insertInfo(map);
        /**
         * 保存行编辑数据
         */
        String listStr1 = "";
        if (map.get("casualtiesList") != null) {
            listStr1 = map.get("casualtiesList").toString();
        }
        List<Map> list1 = JSONArray.parseArray(listStr1, Map.class);
        for (Map m : list1) {
            String uuid = IDGenerator.getId();
            m.put("id", uuid);
            m.put("addTime", DateTimeUtil.formatterDateTime());
        }
        String listStr2 = "";
        if (map.get("handlingSuggestionList") != null) {
            listStr2 = map.get("handlingSuggestionList").toString();
        }
        List<Map> list2 = JSONArray.parseArray(listStr2, Map.class);
        for (Map m : list2) {
            String uuid = IDGenerator.getId();
            m.put("id", uuid);
            m.put("addTime", DateTimeUtil.formatterDateTime());
        }
        String listStr3 = "";
        if (map.get("precautionList") != null) {
            listStr3 = map.get("precautionList").toString();
        }
        List<Map> list3 = JSONArray.parseArray(listStr3, Map.class);
        for (Map m : list3) {
            String uuid = IDGenerator.getId();
            m.put("id", uuid);
            m.put("addTime", DateTimeUtil.formatterDateTime());
        }
        Map<String, Object> map1 = new HashedMap();
        map1.put("list", list1);
        map1.put("accidentReportId", map.get("id"));
        Map<String, Object> map2 = new HashedMap();
        map2.put("list", list2);
        map2.put("accidentReportId", map.get("id"));
        Map<String, Object> map3 = new HashedMap();
        map3.put("list", list3);
        map3.put("accidentReportId", map.get("id"));
        int result1 = 0;
        int result2 = 0;
        int result3 = 0;
        if (result >= 1) {
            result1 = this.casualtiesMapper.insertInfo(map1);
            result2 = this.handlingSuggestionMapper.insertInfo(map2);
            result3 = this.precautionMapper.insertInfo(map3);
        }
        return true;
    }

    @Override
    @Transactional
    public boolean editInfo(Map<String, Object> map) {
        int result = this.accidentReportMapper.updateInfo(map);
        //删除数据
        String accidentReportId = map.get("id").toString();
        if (result >= 1) {
            this.casualtiesMapper.deleteInfo(accidentReportId);
            this.handlingSuggestionMapper.deleteInfo(accidentReportId);
            int a = this.precautionMapper.deleteInfo(accidentReportId);
        }
        /**
         * 保存行编辑数据
         */
        String listStr1 = "";
        if (map.get("casualtiesList") != null) {
            listStr1 = map.get("casualtiesList").toString();
        }
        List<Map> list1 = JSONArray.parseArray(listStr1, Map.class);
        for (Map m : list1) {
            String uuid = IDGenerator.getId();
            m.put("id", uuid);
            m.put("addTime", DateTimeUtil.formatterDateTime());
        }
        String listStr2 = "";
        if (map.get("handlingSuggestionList") != null) {
            listStr2 = map.get("handlingSuggestionList").toString();
        }
        List<Map> list2 = JSONArray.parseArray(listStr2, Map.class);
        for (Map m : list2) {
            String uuid = IDGenerator.getId();
            m.put("id", uuid);
            m.put("addTime", DateTimeUtil.formatterDateTime());
        }
        String listStr3 = "";
        if (map.get("precautionList") != null) {
            listStr3 = map.get("precautionList").toString();
        }
        List<Map> list3 = JSONArray.parseArray(listStr3, Map.class);
        for (Map m : list3) {
            String uuid = IDGenerator.getId();
            m.put("id", uuid);
            m.put("addTime", DateTimeUtil.formatterDateTime());
        }
        Map<String, Object> map1 = new HashedMap();
        map1.put("list", list1);
        map1.put("accidentReportId", map.get("id"));
        Map<String, Object> map2 = new HashedMap();
        map2.put("list", list2);
        map2.put("accidentReportId", map.get("id"));
        Map<String, Object> map3 = new HashedMap();
        map3.put("list", list3);
        map3.put("accidentReportId", map.get("id"));
        int result1 = 0;
        int result2 = 0;
        int result3 = 0;
        if (result >= 1) {
            result1 = this.casualtiesMapper.insertInfo(map1);
            result2 = this.handlingSuggestionMapper.insertInfo(map2);
            result3 = this.precautionMapper.insertInfo(map3);
        }
        return true;
    }

    @Override
    public boolean editEducationSituation(Map<String, Object> map) {
        int result = this.accidentReportMapper.updateEducationSituation(map);
        //删除数据
        String accidentReportId = map.get("id").toString();
        if (result >= 1) {
            this.casualtiesMapper.deleteInfo(accidentReportId);
            this.handlingSuggestionMapper.deleteInfo(accidentReportId);
            int a = this.precautionMapper.deleteInfo(accidentReportId);
        }
        /**
         * 保存行编辑数据
         */

        String listStr3 = "";
        if (map.get("precautionList") != null) {
            listStr3 = map.get("precautionList").toString();
        }
        List<Map> list3 = JSONArray.parseArray(listStr3, Map.class);
        for (Map m : list3) {
            String uuid = IDGenerator.getId();
            m.put("id", uuid);
            m.put("addTime", DateTimeUtil.formatterDateTime());
        }

        Map<String, Object> map3 = new HashedMap();
        map3.put("list", list3);
        map3.put("accidentReportId", map.get("id"));
        int result3 = 0;
        if (result >= 1) {
            result3 = this.precautionMapper.insertInfo(map3);
        }
        return true;
    }

    @Override
    public boolean deleteInfo(String id) {
        int result = this.accidentReportMapper.deleteInfo(id);
        if (result < 1) {
            return false;
        }
        return true;
    }

    /**
     * 查询月报信息
     */
    public List<Map<String, Object>> queryMonthReport(Map<String, Object> map) {
        return this.accidentReportMapper.queryMonthReport(map);
    }

    @Override
    public boolean editAppState(String id, String appState) {
        int result = this.accidentReportMapper.updateAppState(id, appState);
        if (result < 1) {
            return false;
        }
        return true;
    }

    /**
     * 根据部门id，年度，月份查询列表
     *
     * @param map
     * @return
     */
    public List<Map<String, Object>> queryListToMonthReport(Map<String, Object> map) {
        return this.accidentReportMapper.queryListToMonthReport(map);
    }
}
